<!--
 * @Author: linZeJian
 * @Date: 2022-08-24 10:53:55
 * @LastEditors: linZeJian
 * @LastEditTime: 2022-11-08 09:46:42
 * @Description: 
-->
<template>
  <div v-if="option.display">
    <uni-forms-item
      :label="option.label"
      :required="option.required"
      :rules="[{ required: option.required, errorMessage: '必填项不能为空' }]"
      :name="option.prop"
    >
      <uni-data-select
        v-model="comValue"
        :placeholder="
          option.placeholder ? option.placeholder : '请选择' + option.label
        "
        :style="detailValue ? 'pointer-events: none;' : ''"
        :disabled="detailValue"
        :localdata="dicDataLocal"
      />
    </uni-forms-item>
  </div>
</template>

<script>
export default {
  name: "auni-select",
  props: {
    option: Object,
    value: [String, Number, Array],
  },
  data() {
    return {};
  },
  computed: {
    comValue: {
      get() {
        // console.log("this.option :>> ", this.option);
        // console.log("this.value :>> ", this.value);
        return this.value;
      },
      set(val) {
        // 触发value绑定值的交互与更新
        this.$emit("input", val);
      },
    },
    // 处理详情模式
    detailValue() {
      if (this.option.detail) {
        return true;
      } else {
        return false;
      }
    },
    // 处理props，uni-data-select没有:map="{ text: option.props.label, value: option.props.value }"
    // localdata 属性的格式为数组，数组内每项是对象，需要严格遵循如下格式text	显示文本,value	选中后的值
    dicDataLocal() {
      let option = this.option;
      // console.log('option :>> ', option);
      let dicData = option.dicData;
      let dicDataArr = [];
      dicData.forEach(item => {
        dicDataArr.push({
          text: item[option.props.label],
          value: item[option.props.value],
        })
      })
      return dicDataArr;
    },
  },
  created() {},
  watch: {
    value: {
      deep: true,
      immediate: true,
      handler(newV) {},
    },
  },
  methods: {},
};
</script>

<style></style>
